﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using System.Web;

namespace DLL.Person
{
	public class Component
	{

		public string GetHttp(string url)
		{
			string queryString = "?";

			//foreach (string key in httpContext.Request.QueryString.AllKeys)
			//{
			//        queryString += key + "=" + httpContext.Request.QueryString[key] + "&";
			//}

			queryString = queryString.Substring(0, queryString.Length - 1);

			HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

			httpWebRequest.ContentType = "application/json";
			httpWebRequest.Method = "GET";
			httpWebRequest.Timeout = 20000;

			//byte[] btBodys = Encoding.UTF8.GetBytes(body);
			//httpWebRequest.ContentLength = btBodys.Length;
			//httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);

			HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

			StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), System.Text.Encoding.Default);
			//StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());

			string responseContent = streamReader.ReadToEnd();

			httpWebResponse.Close();
			streamReader.Close();

			return responseContent;
		}

		/// <summary>
		/// 根据唯一码查询人员信息
		/// </summary>
		/// <param name="strPersonID"></param>
		/// <returns></returns>
		public Entity QueryByID(string strPersonID)
		{
			string strUrl = "";
			JavaScriptSerializer serializer = new JavaScriptSerializer();
			Entity clsPerson = new Entity();

			strUrl = "http://172.16.8.80/websrv/ws_server.php?func=getygxx&usysid=" + strPersonID;
			clsPerson = (Entity)serializer.Deserialize(GetHttp(strUrl), clsPerson.GetType());
			clsPerson.usysid = strPersonID;

			clsPerson.usysid = clsPerson.usysid.Trim();
			if (clsPerson.gh != null)
			{
				clsPerson.gh = clsPerson.gh.Trim();
				clsPerson.xm = clsPerson.xm.Trim();
				clsPerson.sfzh = clsPerson.sfzh.Trim().ToUpper();
				clsPerson.depid = clsPerson.depid.Trim();
				clsPerson.depchs = clsPerson.depchs.Trim();

				return clsPerson;
			}
			else
			{
				return null;
			}

			
		}

		/// <summary>
		/// 根据工号、姓名、身份证查询用户
		/// </summary>
		/// <param name="strJobNumber">工号</param>
		/// <param name="strName">姓名</param>
		/// <param name="strIDCard">身份证</param>
		/// <returns></returns>
		public Entity Get(string strJobNumber, string strName, string strIDCard)
		{
			string strUrl = "";
			JavaScriptSerializer serializer = new JavaScriptSerializer();
			Entity clsPerson = new Entity();

			//strJobNumber = HttpUtility.UrlEncode(strJobNumber);
			strName = HttpUtility.UrlEncode(strName, System.Text.Encoding.Default);
			//strIDCard = HttpUtility.UrlEncode(strIDCard);

			strUrl = "http://172.16.8.80/websrv/ws_server.php?func=checkygxx&gh=" + strJobNumber + "&xm=" + strName + "&sfzh=" + strIDCard;
			clsPerson = (Entity)serializer.Deserialize(GetHttp(strUrl), clsPerson.GetType());

			if (clsPerson.usysid == "0")
			{
				return null;
			}
			else
			{
				return QueryByID(clsPerson.usysid);
			}
		}

	}

}
